package com.york.security.security;

import com.york.utils.util.Md5;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * @Description:密码处理
 * @Data: 2022/3/22 14:30
 * @Author: yangboxiong
 * @Version
 * @since JDK 1.8
 */
@Component  //将当前类装配到 spring 容器中
@Slf4j
public class DefaultPasswordEncoder implements PasswordEncoder {

    public DefaultPasswordEncoder() {
        this(-1);
    }

    public DefaultPasswordEncoder(int strength) {

    }

    /**
     * 进行Md5加密
     *
     * @param charSequence
     * @return
     */
    @Override
    public String encode(CharSequence charSequence) {
        //使用 Md5 进行加密处理
        String encrypt = Md5.encrypt(charSequence.toString());
        log.info("加密后的MD5:{}", encrypt);
        return encrypt;
    }

    /**
     * 进行密码比对
     *
     * @param charSequence    加密之后的密码
     * @param encodedPassword 传输过来的密码，进行加密后比对
     * @return
     */
    @Override
    public boolean matches(CharSequence charSequence, String encodedPassword) {
        //将编码后的 md5 进行比较
        String encrypt = Md5.encrypt(charSequence.toString());
        log.info("要比对的MD5：{}", encrypt);
        return encodedPassword.equals(encrypt);
    }
}
